package com.pms.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pms.entity.Goal;
import com.pms.entity.Project;
import com.pms.entity.Task;
import com.pms.entity.Users;
import com.pms.service.TaskService;
import com.pms.service.UserService;
import com.pms.util.GetSession;

public class ToEditTask extends HttpServlet {

	
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String taskId = request.getParameter("taskId");
		TaskService taskServ = new TaskService();
		Task task = taskServ.findTaskById(taskId);
		request.setAttribute("task", task);
		UserService userServ = new UserService();
		List<Users> userList = userServ.findAllUsers();
		request.setAttribute("userList", userList);
		request.getRequestDispatcher("/WEB-INF/editTask.jsp").forward(request, response);
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String taskId = request.getParameter("taskId");
		String taskName = request.getParameter("taskName");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String taskContent = request.getParameter("taskContent");
		String level = request.getParameter("level");
		String state = request.getParameter("state");
		double rate = Double.valueOf(request.getParameter("rate")).intValue();
		Users user = (Users)GetSession.getSession(request).getAttribute("user");
		//System.out.println(user);
		String userid = user.getUser_id();
		Project pro = (Project)GetSession.getSession(request).getAttribute("pro");
		//System.out.println(pro);
		String proid = pro.getPro_id();
		Goal goal = (Goal)GetSession.getSession(request).getAttribute("goal");
		String goalid = goal.getGoal_id();
		
		Task task = new Task();
		task.setTask_name(taskName);
		task.setTask_starttime(startTime);
		task.setTask_endtime(endTime);
		task.setTask_context(taskContent);
		task.setLevel(level);
		task.setState(state);
		task.setRate(rate);
		task.setGoal_id(goalid);
		task.setPro_id(proid);
		task.setUser_id(userid);
		
		TaskService taskServ = new TaskService();
		taskServ.updateTaskById(task,taskId);
		
		response.sendRedirect("toTask.jspx");
	}

}
